<?php


namespace app\backend\controller\v1;

use app\backend\validate\AdvertiseValidate;
use app\common\model\Advertise as AdvertiseModel;
use think\exception\ValidateException;
use think\facade\Filesystem;

class Advertise extends BaseController
{
    /**
     * 广告列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //广告表
        $model = new AdvertiseModel();
        //where条件
        $where = input('keyword');
        //获取列表
        $list = $model
            //排序
            ->order('id', 'asc')
            ->field('*')
            ->where(function ($query) use ($where) {
                $query->where('keyword', 'like', '%' . $where . '%');
            })->paginate(10);
        //分页
        $page = $list->render();
        foreach ($list as &$v) {
            $v['image'] = config("app.domain_url") . str_replace('\\', '/', $v['image']);
        }
        return sucessMsg(200, '广告图获取成功！', ['list' => $list, 'total' => count($list), 'page' => $page]);
    }

    /**
     * 显示创建轮播表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $model = new AdvertiseModel();
        $data = input();
        $img = $file = request()->file('image');
        try {
            validate(AdvertiseValidate::class)->scene('create')->check($data);
            if (empty($img)) {
                return errMsg(422, '请上传图片呀!');
            }
            $saveName = Filesystem::disk('public')->putFile('/uploads', $file);
            $data['image'] = $saveName;
            $data['image'] = str_replace('\\', '/', $data['image']);
            if ($model->save($data)) {
                return sucessMsg(200, '创建成功！');
            }
            return errMsg(500, '创建失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }


    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg(400, '请选择要修改的id！');
        }
        $data = input();
        $img = $file = request()->file('image');
        try {
            validate(AdvertiseValidate::class)->scene('update')->check($data);
            if (!empty($img)) {
                $saveName = Filesystem::disk('public')->putFile('/uploads', $file);
                $data['image'] = $saveName;
                $data['image'] = str_replace('\\', '/', $data['image']);
            }
            if (AdvertiseModel::update($data, ['id' => $id])) {
                return sucessMsg(200, '修改成功！');
            }
            return errMsg(500, '修改失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(400, '请选择要删除数据id！');
        }
        if (AdvertiseModel::destroy($id)) {
            return sucessMsg(200, '删除成功！');
        }
        return errMsg(500, '删除失败！');
    }
}